package xsltsample;

import java.io.FileWriter;
import java.io.IOException;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.transform.JDOMResult;
import org.jdom.transform.JDOMSource;

public class Main {

    public static void main(String[] args) throws JDOMException, IOException, TransformerConfigurationException, TransformerException {

        try {

            System.out.print("XML Dokument: ");

            byte[] buffer = new byte[255];
            System.in.read(buffer);

            String xmlFilename = new String(buffer).trim();

            System.out.print("XSL Dokument: ");
            System.in.read(buffer);

            String xslFilename = new String(buffer).trim();

            Document   doc        = new SAXBuilder().build(xmlFilename);
            Source     xmlFile    = new JDOMSource( doc );
            JDOMResult htmlResult = new JDOMResult();

            Transformer transformer = TransformerFactory.newInstance().newTransformer(
                new StreamSource(xslFilename) );

            transformer.transform( xmlFile, htmlResult );

            Format format = Format.getPrettyFormat();
            format.setEncoding("iso-8859-1");

            XMLOutputter xmlOutputter = new XMLOutputter(format);

            FileWriter writer = new FileWriter(xmlFilename.substring(0, xmlFilename.lastIndexOf(".")) + ".html");
            
            xmlOutputter.output( htmlResult.getDocument(), writer);

            writer.close();

        } catch (Exception e) {
            System.out.println("Es ist ein Fehler aufgetreten");
            e.printStackTrace();
        }
    }

}
